/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package server;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;



public  class  Mapper {


    List<MappedHost> hosts = new ArrayList<MappedHost>();

    List<MappedContext> contexts = new ArrayList<MappedContext>();

    List<MappedWrapper> wrappers = new ArrayList<MappedWrapper>();

    Map hostMap = new HashMap();


    protected abstract static class MapElement<T> {

        public final String name;
        public final T object;

        public MapElement(String name, T object) {
            this.name = name;
            this.object = object;
        }
    }


    protected static final class MappedHost extends MapElement<List> {


        public MappedHost(String name, List mappedContext) {
            super(name, mappedContext);
        }
    }

    public void addMappedWrapper(String name, HttpServlet servlet) {
        MappedWrapper map = new MappedWrapper(name, servlet);
        wrappers.add(map);

    }

    public void addMappedContext(String name, List mappedWrappers) {
        MappedContext map = new MappedContext(name, mappedWrappers);
        contexts.add(map);
    }

    public void addMappedHost(String name, List mappedContexts) {
        MappedHost mappedHost = new MappedHost(name, mappedContexts);
        hosts.add(mappedHost);
    }

    public void addHost(Integer port, List hosts) {
        hostMap.put(port, hosts);
    }

    public HttpServlet getSevlet(String url) {
        String[] s = url.split("/");
        String hostname = s[0].split(":")[0];
        String contextname = s[1];
        String wrappername = s[2];
        List mappeHost = (List) hostMap.get(Integer.parseInt(s[0].split(":")[1]));
        List mappedContextList = find(mappeHost, hostname);
        List mappedWrapperList = find(mappedContextList, contextname);
        for (int i = 0; i <mappedWrapperList.size() ; i++) {
            MappedWrapper mappedWrapper = (MappedWrapper)mappedWrapperList.get(i);
            if((contextname+"/"+wrappername).equals(mappedWrapper.name)){
                return mappedWrapper.object;
            }

        }
        return null;
    }

    protected static final class MappedContext extends MapElement<List> {
        public MappedContext(String name, List MappedWrapper) {
            super(name, MappedWrapper);
        }
    }

    protected static class MappedWrapper extends MapElement<HttpServlet> {
        public MappedWrapper(String name, HttpServlet servlet) {
            super(name, servlet);
        }
    }


    private static List  find(List<MapElement<List>> list, String name) {
        for (int i = 0; i <list.size() ; i++) {
            MapElement<List> listMapElement = list.get(i);
            if(name.trim().equals(listMapElement.name)){
                return listMapElement.object;
            }

        }
     return null;
    }

}



